package com.bj.disc.ui.login.fragment

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.view.View
import com.ai3d.rfid.R
import com.ai3d.rfid.base.fragment.BaseVmVdbFragment
import com.ai3d.rfid.common.proxy.ToastProxy
import com.ai3d.rfid.common.utils.HandlerUtils
import com.ai3d.rfid.databinding.FragmentPwdSetBinding
import com.ai3d.rfid.databinding.ItemPwdSetBinding
import com.bj.disc.activity.MainActivity
import com.bj.disc.bean.PwdResponse
import com.bj.disc.ui.login.vm.LoginViewModel
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder


/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 16:53
 * @Description : 登录
 *
 * TODO   初次设置密码UI
 * 主页TabUI 设置页面UI
 * 报告UI
 * 登录接口可以测试一下
 *
 */
class PwdFragment : BaseVmVdbFragment<LoginViewModel, FragmentPwdSetBinding>() {
    private val TAG = "LoginFragment"
    val action: Action by lazy { Action() }

    var list = ArrayList<PwdResponse>()
    var result = arrayOf(-1, -1, -1)
    var confirmResult = arrayOf(-1, -1, -1)
    val adapter: ItemAdapter by lazy { ItemAdapter() }
    var type=0
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        mDatabind.action = action

    }


    override fun initData() {
        super.initData()
        for (i in 0..15) {
            list.add(PwdResponse("https://lanhuapp.com/be6ff181-c5de-443e-82cd-933505b34910"))
        }

        adapter.setNewInstance(list)
        mDatabind.rvSelectPwd.adapter = adapter
    }

    inner class ItemAdapter : LoadMoreModule,
        BaseQuickAdapter<PwdResponse, BaseDataBindingHolder<ItemPwdSetBinding>>(
            R.layout.item_pwd_set
        ) {

        private var action = Action()
        override fun convert(
            holder: BaseDataBindingHolder<ItemPwdSetBinding>,
            item: PwdResponse
        ) {
            item.realPosition = holder.adapterPosition
            holder.dataBinding?.item = item
            holder.dataBinding?.action = action
            holder.dataBinding?.executePendingBindings()
            holder.dataBinding?.ivIcon?.let {
                Glide.with(requireActivity()).load(item.url).into(it)
            }


        }


        inner class Action {

            fun select(item: PwdResponse) {
                if (item.isSelected) {
                    // 取消选择
                    item.isSelected = false
                    if(type==0){
                        result.forEachIndexed { index, it ->
                            if (it == item.realPosition) {
                                result[index] = -1
                            }
                        }
                    }else{
                        confirmResult.forEachIndexed { index, it ->
                            if (it == item.realPosition) {
                                confirmResult[index] = -1
                            }
                        }
                    }

                } else {
                    // 如果已经选择了3个，则不能再选择
                    var allSelect = true
                    if(type==0) {
                        result.forEach {
                            if (it == -1) {
                                allSelect = false
                            }
                        }
                    }else{
                        confirmResult.forEach {
                            if (it == -1) {
                                allSelect = false
                            }
                        }
                    }
                    if (!allSelect) {
                        item.isSelected = true
                        if(type==0) {
                            run out@{
                                result.forEachIndexed { index, it ->
                                    if (it == -1) {
                                        result[index] = item.realPosition
                                        item.selectedPosition = index
                                        return@out
                                    }
                                }
                            }
                        }else{
                            run out@{
                                confirmResult.forEachIndexed { index, it ->
                                    if (it == -1) {
                                        confirmResult[index] = item.realPosition
                                        item.selectedPosition = index
                                        return@out
                                    }
                                }
                            }
                        }
                    }
                }
                notifyDataSetChanged()
            }


        }


    }


    override fun createObserver() {

    }

    override fun onResume() {
        super.onResume()

    }

    override fun onPause() {
        super.onPause()

    }


    override fun onDestroy() {
        super.onDestroy()
        HandlerUtils.removeCallbacksAndMessages()
    }


    inner class Action {

        fun goOn(){
            //判断是否选中了3个
            var allSelect = true
            result.forEach {
                if(it==-1){
                    allSelect=false
                }
            }
            if(!allSelect){
                ToastProxy.showShort("请选择三张图片作密码")
                return
            }
            type=1
            list.forEach {
                it.isSelected = false
            }
            adapter.setNewInstance(list)
            adapter.notifyDataSetChanged()
            mDatabind.llConfirm.visibility=View.VISIBLE
            mDatabind.tvGoOn.visibility=View.GONE
        }
        fun confirm(){
            var isRight=true
            for(i in 0..2){
                if(result[i]!=confirmResult[i]){
                    isRight=false
                }
            }
            if(isRight){
                MainActivity.startMainActivity(requireActivity())
            }else{
                ToastProxy.showShort("两次密码不一样")
            }

        }
        fun goBack(){
           requireActivity().finish()
        }
        fun copy() {
            val clipboardManager =
                context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            // 创建ClipData对象
            val clipData = ClipData.newPlainText("label", "cccc")
            // 将ClipData对象放入剪贴板
            clipboardManager.setPrimaryClip(clipData)
            // 提示用户已成功复制
            ToastProxy.showShort("已复制到剪贴板")
        }


    }
}